<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/10/26
 * Time: 15:31
 * Comment: ''
 */

namespace App\Repository;

use App\Models\MerchantsCastingLog;
use Illuminate\Database\Eloquent\Builder;

class MerchantsCastingLogRepo extends BaseRepo
{
    /**
     * 获取第三方流水号
     * @param string $serial_number
     * @return Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-26 15:32
     */
    public function getBySerialNumber(string $serial_number)
    {
        return MerchantsCastingLog::query()->where('serial_number',$serial_number)->first();
    }

    /**
     * 获取撤销申请单号
     * @param string $revoke_number
     * @return Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-26 16:52
     */
    public function getByRevokeNumber(string $revoke_number)
    {
        return MerchantsCastingLog::query()->where('revoke_number',$revoke_number)->first();
    }

    /**
     * 保存流水记录
     * @param array $data
     * @param int $id
     * @return bool|int
     * @author shen.zk
     * @date 2023-10-27 17:29
     */
    public function saveLog(array $data,int $id = 0)
    {
        if ($id)
        {
            return MerchantsCastingLog::query()->where('id',$id)->update($data);
        }else{
            return MerchantsCastingLog::query()->insert($data);
        }

    }

    /**
     * 获取商户铸造数量变更日志
     * @param array $where
     * @return Builder
     * @author shen.zk
     * @date 2023-10-26 17:43
     */
    public function getMerchantsCastingLog(array $where)
    {
        $query = MerchantsCastingLog::query()->orderByDesc('create_time');
        if ($where['mid'])
            $query->where('access_id',$where['mid']);
        return $query;
    }
}
